/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wumpusworld;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;

/**
 *
 * @author Jedrek
 */
public class PlotCanvas extends Canvas{
    
    public static Color backgroundColor = Color.white;
    public static Color plotColor = Color.blue;
    public static Color frameColor = Color.gray;
    
    public static int leftMargin = 50;
    public static int rightMargin = 20;
    public static int topMargin = 20;
    public static int bottomMargin = 20;
    
    float[] data = null;
    
    public PlotCanvas(){
    
    }
    
    @Override
    public void paint(Graphics g){
        Image backBuffer = createImage(getWidth(), getHeight());
        
        drawPlot((Graphics2D)(backBuffer.getGraphics()));
        
        g.drawImage(backBuffer, 0, 0, this);
    }
    
    public void update(Graphics g){
        paint(g);
    }
    
    public void drawPlot(Graphics2D g){
        float maxVal = 0;
        float minVal = 0;
        float xScale = 1f;
        float yScale = 1f;
        
        g.setColor(Color.getHSBColor(0f, 0f, 0.95f));
        g.fillRect(0, 0, getWidth(), getHeight());
        
        g.setColor(backgroundColor);
        g.fillRect(leftMargin, topMargin, getWidth()-rightMargin-leftMargin, getHeight()-bottomMargin-topMargin);
        
        g.setColor(frameColor);
        g.drawLine(leftMargin, topMargin-5, leftMargin, getHeight() - (bottomMargin - 5));
        g.drawLine(getWidth() - rightMargin, topMargin, getWidth()-rightMargin, getHeight() - bottomMargin);
        g.drawLine(leftMargin-5, topMargin, getWidth()-rightMargin, topMargin);
        g.drawLine(leftMargin-5, getHeight() - bottomMargin, getWidth()-rightMargin, getHeight() - bottomMargin);
        
        if(data == null)
            return;
        if(data.length <= 1)
            return;
        
        minVal = data[0];
        minVal = data[0];
        
        for( int i = 1; i<data.length; i++ ){
            if(data[i]>maxVal)
                maxVal = data[i];
            if(data[i]<minVal)
                minVal = data[i];
        }
        
        if(maxVal > minVal)
            yScale = ((float)getHeight() - topMargin - bottomMargin)/(maxVal - minVal);
        
        xScale = ((float)getWidth() - leftMargin - rightMargin)/(data.length-1);
        
        String maxValString = String.format("%.6s", maxVal);
        String minValString = String.format("%.6s", minVal);
        
        FontMetrics fontMetrics = g.getFontMetrics();
        
        g.setColor(Color.black);
        g.drawString(minValString, leftMargin - fontMetrics.stringWidth(minValString)-6, getHeight()-bottomMargin+fontMetrics.getAscent()/2);
        g.drawString(maxValString, leftMargin - fontMetrics.stringWidth(maxValString)-6, topMargin+fontMetrics.getAscent()/2);
        
        g.setColor(plotColor);
        for(int i = 1; i<data.length; i++)
            g.drawLine((int)((i-1)*xScale+leftMargin),
                    (int)(getHeight() - (data[i-1]-minVal)*yScale - bottomMargin),
                    (int)(i*xScale+leftMargin),
                    (int)(getHeight() - (data[i]-minVal)*yScale - bottomMargin));
    }
    
    public void setData(float[] data){
        this.data = data;
    }
    
    public void setData(ArrayList<Float> data){
        this.data = new float[data.size()];
        
        for(int i = 0; i<this.data.length; i++)
            this.data[i] = data.get(i).floatValue();
    }
}
